<?php

//------------------------
// 权限管理控制器
//-------------------------
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\AuthRule as AuthRuleModel;
use think\Db;

class AuthRule extends AdminBase
{
    protected $auth_rule_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->auth_rule_model        = new AuthRuleModel();
    }



    // 认证规则列表
    public function index(){
    	$model = Db::name('AuthRule');

        $parent = $model->where(array('pid'=>0))->select();

        foreach ($parent as &$data) {
            $child = $model->where(array('pid' => $data['id']))->select();
            $data['child'] = $child;
        }
        unset($data);

        $this->assign('parent',$parent);
        return $this->fetch('index');
    }


    // 添加页面
    public function add(){
    	$pid = $this->request->param('pid',0,'intval');

        $this->assign('pid',$pid);
    	return $this->fetch('add');
    }

    // 编辑页面
    public function edit(){
        $id = $this->request->param('id',0,'intval');
        $data = $this->auth_rule_model->where("id",$id)->find();

        $this->assign('data',$data);
        return $this->fetch('edit');
    }

    /**
     * @desc 添加、编辑操作
     */
    public function add_proc() {
        $data = $this->request->param();
        $id   = $this->request->param('id',0,'intval');

        $validate_result = $this->validate($data, 'AuthRule');
        if ($validate_result !== true) {
            $this->error($validate_result);
        }

        $data['type'] = 1;

        if ($id) {
            // 编辑
            $res = $this->auth_rule_model->allowField(true)->save($data,$id);
            $act = "编辑";
        } else {
            // 添加
            $res = $this->auth_rule_model->allowField(true)->save($data);
            $act = "添加";
        }

        if ($res === false) {
            // 记录日志
            // $logData = array('module' => '规则管理' , 'action' =>  '添加规则' , 'remark' => '添加规则失败' , 'status' => 0);
            // recordActionLog($logData);
            $this->error($this->auth_rule_model->getError());
        } else {
            // 记录日志
            // $logData = array('module' => '规则管理' , 'action' => '添加规则' , 'remark' => '分类编号：' . $id , 'status' => 1);
            // recordActionLog($logData);
            $this->success('规则'.$act.'成功');
        }
    }



    // 删除操作
    public function delete(){
        $id = $this->request->param('id',0,'intval');

        if (!$id) {
            $this->error('请选择需要删除的数据');
        }

        $has = $this->auth_rule_model->where('pid',$id)->select();

        if ($has) {
            $this->error('当前规则下面还有子规则');
        }

        $res = $this->auth_rule_model->where('id',$id)->delete();

        if ($res === false) {
            // 记录日志
            // $logData = array('module' => '设备管理' , 'action' => '删除滚动广告' , 'remark' => '删除滚动广告失败，广告编号：' . $id , 'status' => 0);
            // recordActionLog($logData);
            $this->error($this->auth_rule_model->getError());
        } else{
            // 记录日志
            // $logData = array('module' => '设备管理' , 'action' => '删除滚动广告' , 'remark' => '广告编号：' . $id , 'status' => 1);
            // recordActionLog($logData);
            $this->success('删除成功');
        }
    }



}